package com.artisan.admin.controllers;

import com.artisan.admin.model.accountViewModels.AccountViewModel;
import com.artisan.admin.requestModels.accountListRequestModels.AccountListRequestModel;
import com.artisan.admin.resultModels.MessageBase;
import com.artisan.admin.resultModels.accountResultModels.AccountResultModel;
import com.artisan.admin.resultModels.startPageResultModels.StartPageResultModel;
import com.artisan.domain.dto.AccountDto;
import com.artisan.domain.dto.StartPageDto;
import com.artisan.domain.serviceInterface.IAccountService;
import com.artisan.util.DateUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;

/**
 * 账户控制器
 */
@RequestMapping("/api/account")
@RestController
@Api("账户模块")
@ApiOperation(value = "账户模块")
public class AccountController {
    @Autowired
    private IAccountService accountService;

    /**
     * 获得列表
     *
     * @param req 当前页
     */
    // GET /list
    @ApiOperation(value = "获得列表")
    @ApiResponses(@ApiResponse(code = 200, response = AccountResultModel.class, message = "成功"))
    @RequestMapping(value = {"/list"}, method = {RequestMethod.GET})
    public MessageBase getList(@Valid AccountListRequestModel req) {
        MessageBase result = new MessageBase();
        Page<AccountDto> page = this.accountService.findList(req.getUserName(), req.getProvinceName(), req.getCityName(), req.getAreaName(), req.getStreetName(), req.getCommunityName(), req.getPageIndex(), req.getPageSize());
        result.setPageIndex(page.getNumber());
        result.setPageSize(page.getSize());
        result.setTotalRecord(Math.toIntExact(page.getTotalElements()));

        ArrayList<AccountResultModel> models = new ArrayList<>();

        page.getContent()
                .forEach(
                        dto -> {
                            AccountResultModel model = new AccountResultModel();
                            model.setUserName(dto.getUserName());
                            model.setProvinceName(dto.getProvinceName());
                            model.setCityName(dto.getCityName());
                            model.setAreaName(dto.getAreaName());
                            model.setStreetName(dto.getStreetName());
                            model.setCommunityName(dto.getCommunityName());
                            model.setRoleName(dto.getRoleName());
                            model.setAddDate(DateUtil.timeStamp2Date(dto.getAddDate(), "", true));
                            model.setLastLoginDate(DateUtil.timeStamp2Date(dto.getLastLoginDate(), "", true));
                            model.setStatus(dto.getStatus());

                            models.add(model);
                        });

        result.setData(models);
        return result;
    }

    /**
     * 添加
     *
     * @param model 模型
     */
    // POST
    @ApiOperation(value = "添加")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value = {"/"}, method = {RequestMethod.POST})
    public MessageBase post(@RequestBody AccountViewModel model) {
        MessageBase result = new MessageBase();

        AccountDto dto = new AccountDto();
        dto.setUserName(model.getUserName());
        dto.setPassword(model.getPassword());
        dto.setProvinceName(model.getProvinceName());
        dto.setCityName(model.getCityName());
        dto.setAreaName(model.getAreaName());
        dto.setStreetName(model.getStreetName());
        dto.setCommunityName(model.getCommunityName());
        dto.setRoleId(model.getRoleId());

        int id = this.accountService.add(dto);

        result.setSuccess(id > 0);

        return result;
    }

    /**
     * 获得详情
     *
     * @param id id
     */
    // GET /{1}
    @ApiOperation(value = "获得详情")
    @ApiResponses(@ApiResponse(code = 200, response = AccountResultModel.class, message = "成功"))
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.GET})
    public MessageBase get(@PathVariable int id) {
        MessageBase result = new MessageBase();
        AccountDto dto = this.accountService.find(id);
        AccountResultModel model = new AccountResultModel();
        model.setUserName(dto.getUserName());
        model.setPassword(dto.getPassword());
        model.setProvinceName(dto.getProvinceName());
        model.setCityName(dto.getCityName());
        model.setAreaName(dto.getAreaName());
        model.setStreetName(dto.getStreetName());
        model.setCommunityName(dto.getCommunityName());
        model.setRoleId(dto.getRoleId());
        model.setAddDate(DateUtil.timeStamp2Date(dto.getAddDate(), "", true));
        model.setLastLoginDate(DateUtil.timeStamp2Date(dto.getLastLoginDate(), "", true));
        model.setStatus(dto.getStatus());
        result.setData(model);
        return result;

    }

    /**
     * 更新
     *
     * @param model 模型
     */
    // PUT /{1}
    @ApiOperation(value = "更新")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.PUT})
    public MessageBase put(@PathVariable int id, @RequestBody AccountViewModel model) {
        MessageBase result = new MessageBase();

        AccountDto dto = model.convert();
        dto.setId(id);

        boolean b = this.accountService.update(dto);

        result.setSuccess(b);

        return result;
    }

    /**
     * 删除
     *
     * @param id id
     */
    // DELETE /{1}
    @ApiOperation(value = "删除")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.DELETE})
    public MessageBase delete(@PathVariable int id) {
        MessageBase result = new MessageBase();
        boolean b = this.accountService.delete(id);
        result.setSuccess(b);
        return result;
    }

    /**
     * 修改状态
     * @param ids ids，用逗号隔开
     * @param status 需要修改的状态
     */
    // PATCH /status
    @ApiOperation(value = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",
                    name = "ids",
                    dataType = "String",
                    required = true,
                    value = "ids，用逗号隔开",
                    defaultValue = "1,2,3"),
            @ApiImplicitParam(
                    paramType = "query",
                    name = "status",
                    dataType = "Int",
                    required = true,
                    value = "需要修改的状态",
                    defaultValue = "1")
    })
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value = {"/status"}, method = {RequestMethod.PATCH})
    public MessageBase patchStatus(@RequestParam(value = "ids") String ids, @RequestParam(value = "status") int status){
        MessageBase result = new MessageBase();
        boolean b = this.accountService.updateStatus(ids, status);
        result.setSuccess(b);
        return result;
    }
}
